<template>
  <div class="menu-management-container">
    <el-row :gutter="20">
      <el-col :lg="4" :md="8" :sm="24" :xl="4" :xs="24">
        <vab-card shadow="hover">
          <el-tree
            :data="data"
            :default-expanded-keys="expandDefault"
            highlight-current
            node-key="id"
            :props="defaultProps"
            @node-click="handleNodeClick"
          />
        </vab-card>
      </el-col>
      <el-col :lg="20" :md="16" :sm="24" :xl="20" :xs="24">
        <vab-card shadow="hover">
          <vab-query-form>
            <vab-query-form-top-panel :span="12">
              <el-button
                v-if="MenuAuth('add')"
                :icon="Plus"
                type="primary"
                @click="handleEdit()"
              >
                添加
              </el-button>
            </vab-query-form-top-panel>
          </vab-query-form>
          <el-table
            v-loading="listLoading"
            border
            :data="list"
            default-expand-all
            row-key="path"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          >
            <el-table-column
              label="按钮"
              prop="ButtonName"
              show-overflow-tooltip
            />
            <el-table-column label="编码" prop="Code" show-overflow-tooltip />
            <el-table-column
              label="js事件"
              prop="JsEvent"
              show-overflow-tooltip
            />
            <el-table-column
              label="Action地址"
              prop="ActionEvent"
              show-overflow-tooltip
            />
            <el-table-column
              label="分开线"
              prop="Split"
              show-overflow-tooltip
            />
            <el-table-column label="是否有效" show-overflow-tooltip>
              <template #default="{ row }">
                {{ row.Enabled ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column label="描述" prop="Remark" show-overflow-tooltip />
            <el-table-column label="操作" show-overflow-tooltip width="240">
              <template #default="{ row }">
                <el-button
                  v-if="MenuAuth('edit')"
                  text
                  type="primary"
                  @click="handleEdit(row)"
                >
                  编辑
                </el-button>
                <el-button
                  v-if="MenuAuth('delete')"
                  text
                  type="danger"
                  @click="handleDelete(row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
            <template #empty>
              <!-- <el-image
                class="vab-data-empty"
                :src="require('@/assets/empty_images/data_empty.png')"
              /> -->
              <el-empty class="vab-data-empty" description="暂无数据" />
            </template>
          </el-table>
        </vab-card>
      </el-col>
    </el-row>
    <edit ref="editRef" :tree="list" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getMenuTree, getList } from '@/api/buttonManagement'
  import { Plus } from '@element-plus/icons-vue'
  import { useSettingsStore } from '@/store/modules/settings'
  import { MenuAuth } from '~/src/utils/permission'

  export default defineComponent({
    name: 'ButtonManagement',
    components: {
      Edit: defineAsyncComponent(() =>
        import('./components/buttonManagementEdit')
      ),
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        editRef: null,
        data: [],
        defaultProps: {
          children: 'ChildNodes',
          label: 'text',
        },
        list: [],
        queryForm: {
          moduleId: '',
          category: '1',
          // buttonId: 'string',
        },
        listLoading: true,
        expandDefault: [],
        checkID: '',
      })
      //列表编辑
      const handleEdit = (row) => {
        if (row && row.ModuleId) {
          state['editRef'].showEdit(row, state.queryForm)
        } else {
          state['editRef'].showEdit(state.queryForm.moduleId, state.queryForm)
        }
      }
      //页面刷新
      const settingsStore = useSettingsStore()
      const { theme } = storeToRefs(settingsStore)
      const $pub = inject('$pub')
      const refreshRoute = () => {
        $pub('reload-router-view')
      }
      //列表删除
      const handleDelete = (row) => {
        if (row.ModuleId) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { message } = await doDelete({ buttonId: row.ButtonId })
            //删除列表默认回到第一页
            state.queryForm.pageNo = 1
            $baseMessage(message, 'success', 'vab-hey-message-success')
            await fetchData(state.queryForm)
          })
        }
      }
      //菜单表格
      const fetchData = async (role) => {
        state.listLoading = true
        const {
          data: { data },
        } = await getList({ ...role })
        state.list = JSON.parse(data).rows
        state.listLoading = false
      }
      //树形数据点击
      const handleNodeClick = ({ id }) => {
        state.queryForm.moduleId = id
        fetchData(state.queryForm)
      }
      //获取树形结构下第一个ID
      const getFirstNode = (tree) => {
        if (tree.ChildNodes.length > 0) {
          getFirstNode(tree.ChildNodes[0])
        } else {
          state.queryForm.moduleId = tree.id
          return
        }
      }
      //树形结构数据
      getMenuTree().then(({ data }) => {
        const list = data.data
        state.data = JSON.parse(list)
        //默认选中第一个
        getFirstNode(state.data[0])
        state.expandDefault.push(state.queryForm.moduleId)
        fetchData(state.queryForm)
      })
      // onMounted(() => {
      //   fetchData(state.queryForm)
      // })

      return {
        ...toRefs(state),
        handleEdit,
        handleDelete,
        fetchData,
        handleNodeClick,
        Plus,
        refreshRoute,
        MenuAuth,
      }
    },
  })
</script>

<style lang="scss" scoped>
  $base: '.menu-management';
  #{$base}-container {
    padding: 0 !important;
    background: $base-color-background !important;
  }
</style>
